<?php
namespace Common\Model;

class SysMenuModel extends CommonModel
{
    /**
     * 按父ID查找菜单子项
     * @param integer $parentid   父菜单ID
     * @param integer $with_self  是否包括他自己
     */
    public function admin_menu($parentid, $with_self = false) {
        //父节点ID
        $parentid = (int) $parentid;
        $result = $this->where(array('parentid' => $parentid, 'status' => 1))->order(array("listorder" => "ASC"))->select();

        if ($with_self) {
            $result2[] = $this->where(array('id' => $parentid))->find();
            $result = array_merge($result2, $result);

        }

        //权限检查
        if (sp_get_current_admin_id() == 1) {
            //如果是超级管理员 直接通过
            return $result;
        }else {

            $array = array();
            //查看 当前用户的 权限
            $menus = M("vradmin1.sys_menu_admin", null);

            $menus_admin_two = $menus->where("admin_id = " . $_SESSION['ADMIN_ID'])->field("menu_id")->select();

            $menus_admin = i_array_column($menus_admin_two, 'menu_id');


            foreach ($result as $v) {
                if (in_array($v['menu_id'], $menus_admin)) {
                    $array[] = $v;
                }

//            //方法
//            $action = $v['action'];
//
//            //public开头的通过
//            if (preg_match('/^public_/', $action)) {
//                $array[] = $v;
//            } else {
//
//                if (preg_match('/^ajax_([a-z]+)_/', $action, $_match)){
//
//                    $action = $_match[1];
//                }
//
//                $rule_name=strtolower($v['app']."/".$v['model']."/".$action);
//
//                if ( sp_auth_check(sp_get_current_admin_id(),$rule_name)){
//                    $array[] = $v;
//                }
//
//            }
            }

            return $array;
        }
    }

    /**
     * 菜单树状结构集合
     */
    public function menu_json() {
        $data = $this->get_tree(0);
        return $data;
    }

    //取得树形结构的菜单
    public function get_tree($myid, $parent = "", $Level = 1) {
        $data = $this->admin_menu($myid);
        $Level++;
        if (is_array($data)) {
            $ret = NULL;
            foreach ($data as $a) {
                $id = $a['menu_id'];
                $name = ucwords($a['app']);
                $model = ucwords($a['model']);
                $action = $a['action'];
                //附带参数
                $fu = "";
                if ($a['data']) {
                    $fu = "?" . htmlspecialchars_decode($a['data']);
                }
                $array = array(
                    "icon" => $a['icon'],
                    "id" => $id . $name,
                    "name" => $a['name'],
                    "parent" => $parent,
                    "url" => U("{$name}/{$model}/{$action}{$fu}", array("menuid" => $id)),
                    'lang'=> strtoupper($name.'_'.$model.'_'.$action)
                );



                $ret[$id . $name] = $array;
                $child = $this->get_tree($a['menu_id'], $id, $Level);
                //由于后台管理界面只支持三层，超出的不层级的不显示
                if ($child && $Level <= 3) {
                    $ret[$id . $name]['items'] = $child;
                }

            }
            return $ret;
        }

        return false;
    }

    /**
     * 得到某父级菜单所有子菜单，包括自己
     * @param number $parentid
     */
    public function get_menu_tree($parentid=0){
        $menus=$this->where(array("parentid"=>$parentid))->order(array("listorder"=>"ASC"))->select();

        if($menus){
            foreach ($menus as $key=>$menu){
                $children=$this->get_menu_tree($menu['id']);
                if(!empty($children)){
                    $menus[$key]['children']=$children;
                }
                unset($menus[$key]['id']);
                unset($menus[$key]['parentid']);
            }
            return $menus;
        }else{
            return $menus;
        }

    }

}